package code.c03_stackQueue.linkStack;

import com.sun.org.apache.bcel.internal.generic.NEW;
import sun.awt.util.IdentityLinkedList;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/2 22:20
 * @package code.c03_stackQueue.linkStack
 * @description 假设用不带头结点的的单链表作为链栈，请设计栈的4个基本运算算法
 */

public class Exam_3_9 {

}

class LinkStackClass1<E> {
    LinkNode<E> head;

    public LinkStackClass1() {
        head = null;
    }

    /**
     * 判断栈是否为空
     * @return
     */
    public boolean empty(){
        return head == null;
    }

    /**
     * 入栈
     * @param e
     */
    public void push(E e){
        LinkNode<E> s = (LinkNode<E>) new LinkNode<>(e);
        s.next = head;
        head = s;
    }

    /**
     * 出栈
     * @return
     */
    public E pop(){
        if (empty()){
            throw new IllegalArgumentException("栈空");
        }
        E data = head.data;
        head = head.next;
        return data;
    }

    /**
     * 取栈顶元素
     * @return
     */
    public E peek(){
        if (empty()){
            throw new IllegalArgumentException("栈空");
        }
        return head.data;
    }
}